package search

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"wechat/global"
	"wechat/handle/model"
	"wechat/handle/request"
)

//信息发送

func AddMessage(c *gin.Context) {
	var form request.AddMess
	if err := c.ShouldBind(&form); err != nil {
		c.JSONP(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "参数错误",
		})
		return
	}

	var message model.Message
	message = model.Message{
		UserId:   form.UserId,
		FriendId: form.FriendId,
		Content:  form.Content,
		Status:   form.Status,
	}

	if err := message.AddMessage(global.DB); err != nil {
		c.JSONP(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "信息发送失败",
		})
		return
	}

	c.JSONP(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "信息发送成功",
	})

}

//聊天列表

func ListMess(c *gin.Context) {
	var form request.ListMess
	if err := c.ShouldBind(&form); err != nil {
		c.JSONP(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "参数错误",
		})
		return
	}

	var message model.Message

	listMessage, err := message.ListMessage(global.DB, form.UserId)
	if err != nil {
		c.JSONP(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "聊天记录获取失败",
		})
		return
	}

	c.JSONP(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "聊天记录获取成功",
		"Data": map[string]interface{}{
			"list": listMessage,
		},
	})

}
